<?php
class Admin_ServerController extends Zend_Controller_Action
{
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	public function init(){
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';	

	}
	
	public function indexAction()
	{
		$translate = Zend_Registry::get('Zend_Translate');
		$this->view->title = $translate->_("TITLE_ADMIN_SERVER_INDEX");
		$this->view->headTitle($this->view->title);
		$serverModel = new Admin_Model_Server();
		$listServer = $serverModel->fetchServer();
		$listServer = $listServer->toArray();
		$this->view->listServer = $listServer;
	}
	
	public function insertAction()
	{
		$translate = Zend_Registry::get('Zend_Translate');
		$this->view->title = $translate->_("TITLE_ADMIN_SERVER_INDEX");
		$this->view->headTitle($this->view->title);
		
		$form = new Admin_Form_Server();
		$back = $form->getElement('back');
		$back->setAttrib('onclick', "window.location='" .$this->getFrontController()->getBaseUrl() . "/admin/server/';");
				
		if ($this->_request->isPost()){	
			if ($form->isValid($_POST)){
				$status = $_POST['status'];
				$serverModel = new Admin_Model_Server();
				$process = $serverModel->insertServer($form->getValue('name'), $form->getValue('url_server'), $status);
				if ($process)
				{
					$this->view->note = $translate->_('ADMIN_TEXT_MESSAGE_ADD_SERVER_SUCCESS');
				}else{
					$this->view->note = $translate->_('ADMIN_TEXT_MESSAGE_ADD_SERVER_FAILD');
				}		
			}
		}
		$this->view->form = $form;			
	}
	
	public function deleteitemsAction(){
		$this->_helper->layout()->disableLayout();
		$serverModel = new Admin_Model_Server();
		if($this->_request->isPost()){
			$item = $this->_arrParam['cid'];
			foreach ($item as $k=>$v){
				$serverModel->deleteServer($v);
			}
		}
		$this->_redirect($this->_actionMain);
	}
	
	public function activeAction(){
		$this->_helper->layout()->disableLayout();
		$type = $this->_request->getParam('type',1);
		$id = $this->_request->getParam('id',0);
		if($type>0){
			$type = 1;
		}else{
			$type = 0;
		}
		$serverModel = new Admin_Model_Server();
		$serverModel->update(array('status'=>$type), 'id='.$id);
		$this->_redirect($this->_actionMain);
	}
	
	public function updateAction()
	{
		$translate = Zend_Registry::get('Zend_Translate');
		$this->view->title = $translate->_("TITLE_ADMIN_SERVER_UPDATE");
		$this->view->headTitle($this->view->title);
		
		$serverModel = new Admin_Model_Server();
		$id_server = $this->_getParam('id', 0);		
		
		$form = new Admin_Form_Server();		
		$back = $form->getElement('back');
		$back->setAttrib('onclick', "window.location='" .$this->getFrontController()->getBaseUrl() . "/admin/server/';");
				
		if ($this->_request->isPost()){	
			if ($form->isValid($_POST)){
				$status = $_POST['status'];
				$process = $serverModel->updateServer($id_server, $form->getValue('name'),$form->getValue('url_server'), $status);
				if ($process)
				{
					$this->view->note = $translate->_('ADMIN_TEXT_MESSAGE_UPDATE_SERVER_SUCCESS');
				}else{
					$this->view->note = $translate->_('ADMIN_TEXT_MESSAGE_UPDATE_SERVER_FAILD');
				}		
			}
		}else{
			$data = $serverModel->fetchServer(array('id'=>$id_server));
			$data = $data->toArray();
			if(count($data) > 0){
				$form->populate($data[0]);
			}
		}
		$this->view->form = $form;
	}
	
	public function deleteAction()
	{
		$translate = Zend_Registry::get('Zend_Translate');
		$this->view->title = $translate->_("TITLE_ADMIN_SERVER_DELETE");
		$this->view->headTitle($this->view->title);
		
		$serverModel = new Admin_Model_Server();
		if ($this->getRequest()->isPost()) {
			$del = $this->getRequest()->getPost('del');          
            if ($del == 'Yes') { 
            	$id = $this->getRequest()->getPost('id'); 
            	$result = $serverModel->deleteServer($id);
            }
            $this->_redirect($this->_actionMain);
			
		}else{
			$id = $this->_getParam('id', 0);         
            $data = $serverModel->fetchServer(array('id'=>$id));
            $this->view->menu = $data->toArray();
		}
	}	
}